﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;

namespace Platform.DB
{
    public class MySqlFactory : IDbFactory
    {
        #region 构造

        public MySqlFactory()
        {
            this._connectString = $"server=localhost;user=root;port=3306;password=ndk;DATABASE=AFMT_db;CHARSET=utf8;";
        }

        #endregion

        #region 字段属性

        private  string _connectString;

        public string ConnectString
        {
            get
            {
                return this._connectString;
            }
            set
            {
                _connectString = value;
            }
        }

        #endregion

        #region IDbFactory实现

        public IDbConnection CreateConnection()
        {
            return new MySqlConnection(this._connectString);
        }

        public IDbConnection CreateConnection1(string dbname)
        {
            return new MySqlConnection($"server=localhost;user=root;port=3306;password=ndk;DATABASE={dbname};CHARSET=utf8;");
        }

        public IDbDataAdapter CreateDataAdapter()
        {
            return new MySqlDataAdapter();
        }

        public IDataParameter CreateDataParameter()
        {
            return new MySqlParameter();
        }

        public bool CreateDb(string dbName)
        {
            ConnectString = $"server=localhost;user=root;port=3306;password=ndk;DATABASE={dbName};CHARSET=utf8;";
            string text = this._connectString;
            int length = text.IndexOf("DATABASE", StringComparison.OrdinalIgnoreCase);
            text = text.Substring(0, length);
            IDbConnection dbConnection = null;
            try
            {
                dbConnection = new MySqlConnection(text);
                dbConnection.Open();
                string commandText = "CREATE DATABASE IF NOT EXISTS " + dbName + " DEFAULT CHARACTER SET utf8";
                IDbCommand dbCommand = dbConnection.CreateCommand();
                dbCommand.CommandText = commandText;
                dbCommand.ExecuteNonQuery();
            }
            catch (Exception ex) { }
            finally
            {
                if (dbConnection != null)
                {
                    dbConnection.Close();
                }
            }
            return true;
        }

        #endregion

        #region 共有方法




        #endregion

        public string[] GetDbInfos(string strSql)
        {
            List<string> list = new List<string>();
            IDataReader dataReader = DbAccess.ExecuteQueryReturnDataReader(strSql);
            if (dataReader == null)
            {
                return null;
            }
            string[] result;
            try
            {
                while (dataReader.Read())
                {
                    string @string = dataReader.GetString(0);
                    list.Add(@string);
                }
                result = list.ToArray();
            }
            finally
            {
                dataReader.Close();
            }
            return result;
        }

    }
}
